package com.gitee.zycra.union.api.dao;

import com.gitee.zycra.jdbc.common.BaseDao;
import com.gitee.zycra.jdbc.common.CommonSelector;
import com.gitee.zycra.union.api.common.model.ApiMockTemplateInterface;
import com.gitee.zycra.union.api.common.vo.MockTemplateInterfaceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zycra
 */
@Repository
public class ApiMockTemplateInterfaceDao extends BaseDao<ApiMockTemplateInterface> {

    @Autowired
    private CommonSelector commonSelector;

    /**
     * 插入mock模板接口
     *
     * @param apiMockTemplateInterface 模板对象
     * @return 影响行数
     */
    public int insert(ApiMockTemplateInterface apiMockTemplateInterface) {
        return insertIntoTable(apiMockTemplateInterface);
    }

    /**
     * 根据ID删除
     *
     * @param id 要删除的ID
     * @return 影响行数
     */
    public int updateToDelete(Long id) {
        return updateTableById(new ApiMockTemplateInterface().setId(id).setDeleted(true));
    }

    /**
     * 根据模板ID查询接口
     *
     * @param templateId 模板ID
     * @return 查询结果
     */
    public List<MockTemplateInterfaceVO> selectByTemplateId(Long templateId) {
        return commonSelector.selectByParam(getJdbcTemplate(),
                "SELECT amti.id, amti.interface_id, aif.folder_name, ai.interface_name FROM api_mock_template_interface"
                + " amti INNER JOIN api_interface ai ON amti.interface_id = ai.id AND amti.deleted = 0 AND ai.deleted = 0 INNER JOIN"
                + " api_interface_folder aif ON ai.interface_folder_id = aif.folder_name AND aif.deleted = 0"
                + " WHERE amti.template_id = ?", MockTemplateInterfaceVO.class, List.of(templateId));
    }
}
